Object Oriented Programming (OOP) in CPP

22/9/2022 16:11 - Asia/Calcutta

Object Oriented Programming (OOP) in C++

  1. As the name suggests, it brings the use of "objects" in programming. Basically, they are some guidelines for running efficient programs.
    Let me explain the difference between tradition style and OOP style of programming with an example:
    Traditional: running(); // we don't know who's running.
    OOP: Jack.running(); // here we know who's running. Jack is the object here.

  2. The main aim of OOP is to bind together the data and functions that work on them, so that no other part of the code can access the data, except that function.

CLASS

  1. User defined data type, which holds its own data members and member functions.
  2. Used to define properties/behaviour of objects.
    Example:
                        
class Car
  {
  	// These are some of the properties that every Car share in common.
  	int number_of_wheels;
  	int cost;
  	bool fuel_type;

  	int increase_speed()
  	{ /* some code */ }
  };
                        
                        
  1. Data members are data variables.
  2. Member functions are the functions used to manipulate these variables.
  3. Together they define properties/behaviour of the class.

OBJECTS

  1. Objects are an instance of a class.
  2. When a class is created no memory is allocated, memory allocation done only when object is initialised.
    Example:
                        
 class Car
    {
    	// These are some of the properties that every Car share in common.
    	int number_of_wheels;
    	int cost;
    	bool fuel_type;

    	int increase_speed()
    	{ /* some code */ }
    };
    int main()
    {
    	Car c1; // object of car class initialised.
    }
                        
                        

ENCAPSULATION

  1. Wrapping up of data & function into a single unit.
  2. Also known as INFORMATION HIDING concept.
    Example:
    Suppose I'm using a TV remote. I need to only press the button and it changes channels or increases volume for me. I don't need to know to internal function of the remote. Here class Remote encapsulates them.

ABSTRACTION

  1. Only showing the user/outer world, what is need and hiding other important informations.
  2. Also a kind of Data Hiding.

Encapsulation vs Abstraction:

  • Abstraction is basically a thought process of showing what is needed (basically showing only relevant data).
  • Encapsulation on the other hand is actually bringing down complexity by hiding data. In other words,
    Encapsulation implements Abstraction.
                        
Example:

    class Customer
    {
    	public:
    	string customer_name = "";
    	string customer_id = "";

    	void Add()
    	{
    		validate(); 
    		Add_to_database();
    	}

    	private:
    	void validate()
    	{ /* some code */ }
    	void Add_to_database()
    	{ /* some code */ }
    };

                        
                        
  • The above Customer class, is the membership storage database of a shopping mall.
  • In the above code the private part is irrelevant to the customer, so it is hidden to the outside world. Only the public part is useful to the customer.

How is Abstraction done ?

  1. Using data member visibility (public/private/protected)
  2. Using header files:
    Suppose we use pow() in our code that come under math.h. We don't know the underlying algorithm used (hidden), but we just use it in our codes.

POLYMORPHISM

  1. poly: many, morph: forms. Basically, when one thing has more than one forms.
  2. Basically, more than one function, with same name yet different use.

Types:

  1. Static/ Compile time:
    a. Function overloading:
    Example:
                        
 class C
    {
    	void fun(int x)
    	{...}
    	void fun(double y)
    	{...}
    	void fun(int , int)
    	{...}
    };
                        
                        

b. Operator Overloading

                        
class Complex {
private:
	int real, imag;
public:
	Complex(int r = 0, int i =0)  {real = r;   imag = i;}

	// This is automatically called when '+' is used with
	// between two Complex objects
	// Operator overloading part.
	Complex operator + (Complex const &obj) {
		 Complex res;
		 res.real = real + obj.real;
		 res.imag = imag + obj.imag;
		 return res;
	}
	void print() { cout << real << " + i" << imag << endl; }
};
                        
                        

  1. Dynamic /Runtime:
    a. Virtual Function

INHERITANCE

  1. Capability of a class to derive properties from other class.
    Example:
                        
class Animal 
  {  
     public:  
 	 void eat() 
 	 {   
 		cout<<"Eating..."<< endl;   
 	 }    
  };  
    class Dog: public Animal    
   {    
        public:  
 		void bark()
 		{  
 			cout<<"Barking...";   
 		}    
  };   
                        
                        
  • Here, the class Dog is derived from class Animal. It has properties of Animal, alongwith some additional functionalities.
  • In C++, if a derived class redefines base class member method then all the base class methods with same name become hidden in derived class. i.e --
                        
class Base 
 {
 public:
     int fun()      { cout << "Base::fun() called"; }
     int fun(int i) { cout << "Base::fun(int i) called"; }
 };

 class Derived: public Base 
 {
 public:
     int fun(char x)   { cout << "Derived::fun(char ) called"; }
 };
 int main() 
 {
     Derived d;
     d.fun();
     return 0;
 }
                        
                        

The above code gives error upon compilation, as fun() of base class is not accessible in the derived class.

Modes of Inheritance

  1. Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class.
  2. Protected mode: If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class.
  3. Private mode: If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class.

Types of Inheritance in C++

  1. Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.
  2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes.
  3. Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class.
  4. Hierarchical Inheritance: In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.

ACCESS MODIFIER

  1. Data Hiding.
  2. By default, it is private in class.
  3. Private: Can be accessed by only inside class member functions or the friend function.
  4. Public: Can be accessed by anyone.
  5. Protected: Can be accessed by only inside class member functions or the friend function. In addition, can alos be accessed by the derived class.
FRIEND CLASS and FUNCTION

Important Points

  1. A friend class can access private and protected members of other class in which it is declared as a friend.
  2. A friend function, like friend class can access private and protected members of other class in which it is declared as a friend.
  3. It can be declared as : i) Global Function. ii) A member of other class.
  4. Friendship is NOT inherited. (If base class has a friend function, it doesn't automatically become friend of derived class.)

Friend Class

                        
class Node {
private:
	int key;
	Node* next;
	/* Other members of Node Class */

	// Now class LinkedList can
	// access private members of Node
	friend class LinkedList;
};
                        
                        

Another Example:

                        
#include
using namespace std;

class A
{
	int x;
		public:
			
	A()
	{
		x=10;
	}
	friend class B; //friend class
};

class B
{
	public:
		void display(A &t)
		{
			cout<
#include 
using namespace std;

class Point {
private:
	int x, y;

public:
  Point(){
	x = 0;
	y = 0;
  }	
  Point(int x1, int y1)
	{
		x = x1;
		y = y1;
	}

	// Copy constructor
	Point(const Point& p1)
	{
		x = p1.x;
		y = p1.y;
	}

	int getX() { return x; }
	int getY() { return y; }
};

int main()
{
	Point p0 = Point();
	Point p1(10, 15); // Normal constructor is called here
	Point p2 = p1; // Copy constructor is called here

	// Let us access values assigned by constructors
	cout << "p0.x = " << p0.getX()
		<< ", p0.y = " << p0.getY();
	cout << "\np1.x = " << p1.getX()
		<< ", p1.y = " << p1.getY();
	cout << "\np2.x = " << p2.getX()
		<< ", p2.y = " << p2.getY();

	return 0;
}
                        
                        
  • Default copy constructor creates a shallow copy of the objects, whereas in user defined copy constructor, a deep copy is created.

DESTRUCTOR

  1. Destructor is a member function (bearing same name as the construction with a ~ before) which destroys or deletes an object. [~ constructor_name]
  2. Should always be declared publicly, and cannot be declared as const or static.
  3. Programmer can't access the address of the destructor.

VIRTUAL FUNCTION

  1. Member function, declared within a base class, and is redefined within the derived class.
  2. When we refer to a derived class object using pointer to a base class, we can call the virtual function for that object & execute the derived class's version of the function.
  3. Type of Runtime polymorphism.

What if We Do not define virual for an overring method?
So with base class pointer dervieved class object the method of derived class will not be overridden. It calls the base class, if we do not use virtual keyword.
Example:

                        
#include 
using namespace std;

class A {
public:
	virtual void fun() { cout << "\n A::fun() called "; }  //note 1
};

class B : public A {
public:
	void fun() { cout << "\n B::fun() called "; }
};

class C : public B {
public:
	void fun() { cout << "\n C::fun() called "; }
};

int main()
{
	// An object of class C
	C c;

	// A pointer of class B pointing
	// to memory location of c
	B* b = &c;

	// this line prints "B::fun() called" if note1 is not virtual
  	// otherwise it prints "C::fun() called"
	b->fun();

	getchar(); // to get the next character
	return 0;
}
                        
                        

ABSTRACT CLASS

  1. Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class.
    Example, let Shape be a base class. We cannot provide implementation of function draw() in Shape, but we know every derived class must have implementation of draw().
  2. Class is Abstract, if we have atleast one pure virtual function.

PURE VIRTUAL FUNCTION

  1. Also called Absract function.
  2. A pure virtual function in c++, is a virtual function for which we can have implementation, but we must override that function in the derived class, otherwise the derived class will also become abstract class.
    Example
                        
class X
  {
  	public:
  	virtual void show() = 0; // pure virtual func
  };
                        
                        

Comments

Submit
0 Comments